using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;

namespace ProjectEuler.Core
{
    public class Problem22 : IProjectEulerProblem
    {
        protected IList<string> GetWords()
        {
            return File.ReadAllText("GivenFiles\\names.txt")
                .Replace("\"", "")
                .Split(',')
                .OrderBy(name => name)
                .ToList();
        }

        protected int WordScore(string word)
        {
            return word.Sum(c => c - ('A' - 1));
        }

        protected IList<int> GetAllWordScores()
        {
            var wordscores = new List<int>();
            var allwords = GetWords();
            for(int i=0;i<allwords.Count;i++)
            {
                wordscores.Add(WordScore(allwords[i]) * (i+1));
            }
            return wordscores;
        }

        public int Number
        {
            get { return 22; }
        }

        public string Description
        {
            get { return "What is the total of all the name scores in the given file?"; }
        }

        public string Answer
        {
            get { return GetAllWordScores().Sum().ToString(); }
        }
    }

    [TestFixture]
    public class Problem22Tests : Problem22
    {
        [Test]
        public void Test_that_MATT_word_value_is_54()
        {
            // 13 + 1 + 20 + 20
            Assert.AreEqual(13+1+20+20, WordScore("MATT"));
        }

        [Test]
        public void Can_read_names_from_files()
        {
            Assert.IsTrue(GetWords().Count > 5000);
        }
    }
}